/*
 * Copyright (c) 2015 [1076559197@qq.com | tchen0707@gmail.com]
 *
 * Licensed under the Apache License, Version 2.0 (the "License”);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.jasonsun.sharelib;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.PopupWindow;
import android.widget.TextView;

/**
 * Author:  Tau.Chen
 * Email:   1076559197@qq.com | tchen0707@gmail.com
 * Date:    15/10/19 16:22
 * Description:
 */
public class PopupDialogShare extends PopupWindow {

    private FrameLayout mRootContainer;
    private TextView mShareWechatCircle;
    private TextView mShareWechat;
    private TextView mShareQq;
    private TextView mShareQzone;
    private TextView mShareWeibo;
    private ImageButton mClose;

    private View mContentView;

    public PopupDialogShare(Activity context, onShareModelSelectedListener listener) {
        init(context, listener);
    }

    private void findViews(View v) {
        mRootContainer = (FrameLayout) v.findViewById(R.id.share_root_container);
        mShareWechatCircle = (TextView) v.findViewById(R.id.share_wechat_circle);
        mShareWechat = (TextView) v.findViewById(R.id.share_wechat);
        mShareQq = (TextView) v.findViewById(R.id.share_qq);
        mShareQzone = (TextView) v.findViewById(R.id.share_qzone);
        mShareWeibo = (TextView) v.findViewById(R.id.share_weibo);
        mClose = (ImageButton) v.findViewById(R.id.share_close);
    }

    private void init(final Activity context, onShareModelSelectedListener listener) {
        mOnShareModelSelectedListener = listener;

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        mContentView = inflater.inflate(R.layout.dialog_share, null);
        findViews(mContentView);

        setContentView(mContentView);
        setWidth(DencityUtil.getDisplayWidth(context));
        setHeight(DencityUtil.getDisplayHeight(context));

        setBackgroundDrawable(new ColorDrawable(context.getResources().getColor(R.color.transparent)));

        setFocusable(true);
        setOutsideTouchable(true);
        update();

        setAnimationStyle(R.style.PopupWindowBottomStyle);

        mRootContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mRootContainer) {
                    mRootContainer.setBackgroundResource(R.drawable.transparent);
                    mRootContainer.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            dismiss();
                        }
                    }, 100);
                }
            }
        });

        mShareQq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mRootContainer) {
                    mRootContainer.setBackgroundResource(R.drawable.transparent);
                    mRootContainer.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            dismiss();
                        }
                    }, 100);
                }

                if (null != mOnShareModelSelectedListener) {
                    mOnShareModelSelectedListener.onSelected(ShareInstance.SharePlatFormId.QQ_FRINEDS);
                }
            }
        });

        mShareQzone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mOnShareModelSelectedListener) {
                    mOnShareModelSelectedListener.onSelected(ShareInstance.SharePlatFormId.QZONE);
                }
//                dismiss();
            }
        });

        mShareWechat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mRootContainer) {
                    mRootContainer.setBackgroundResource(R.drawable.transparent);
                    mRootContainer.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            dismiss();
                        }
                    }, 100);
                }

                if (null != mOnShareModelSelectedListener) {
                    mOnShareModelSelectedListener.onSelected(ShareInstance.SharePlatFormId.WEIXIN_FRIENDS);
                }
            }
        });

        mShareWechatCircle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mRootContainer) {
                    mRootContainer.setBackgroundResource(R.drawable.transparent);
                    mRootContainer.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            dismiss();
                        }
                    }, 100);
                }

                if (null != mOnShareModelSelectedListener) {
                    mOnShareModelSelectedListener.onSelected(ShareInstance.SharePlatFormId.WEIXIN_CIRCLE);
                }
            }
        });

        mShareWeibo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mOnShareModelSelectedListener) {
                    mOnShareModelSelectedListener.onSelected(ShareInstance.SharePlatFormId.XINLANG_WEIBO);
                }
                dismiss();
            }
        });

        mClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mRootContainer) {
                    mRootContainer.setBackgroundResource(R.drawable.transparent);
                    mRootContainer.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            dismiss();
                        }
                    }, 100);
                }
            }
        });
    }

    private onShareModelSelectedListener mOnShareModelSelectedListener;

    public interface onShareModelSelectedListener {
        void onSelected(ShareInstance.SharePlatFormId shareTo);
    }

    public void showPopup(View targetView) {
        showAtLocation(targetView, Gravity.CENTER, 0, 0);
        targetView.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (null != mRootContainer) {
                    mRootContainer.setBackgroundResource(R.drawable.drawable_alpha_20_black);
                }
            }
        }, 400);
    }

}
